<?php


namespace app\crontab\controller\always;


use app\common\lib\RespCode;
use custom\Log;
use push\Aurora2;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Exception;

/**
 * 推送提醒房源
 * Class PushRemindHosue
 * @package app\crontab\controller
 */
class PushRemindHosue extends Command {

    protected function configure() {
        $this->setName('PushRemindHosue');
    }

    protected function execute(Input $input, Output $output) {
        echo "runing\n";
        $agent_remind = db('agent_remind')
            ->where('is_push', 0)
            ->select();
        foreach ($agent_remind as $k => $v) {
            try {
                $registration_id = db('company_agent')->where('id', $v['agent_id'])->value('registration_id');
                switch ($v['remind_type']) {
                    case 1://关注的房源
                        $house = db('house')
                            ->alias('h')
                            ->leftJoin('house_auction ha', 'ha.id =h.auction_id')
                            ->leftJoin('house_village hv', 'hv.id =h.village_id')
                            ->where('h.is_shelf', 1)
                            ->where('h.id', $v['remind_id'])
                            ->where('h.deleted', 0)
                            ->where('ha.auction_status', 1)
                            ->whereRaw('(ha.start_time - UNIX_TIMESTAMP(now()))/60/60/24 < 1')
                            ->field('hv.name as vname,h.bedroom,h.livingroom,ha.start_time')
                            ->find();
                        //有即将开拍房源
                        if ($house) {
                            //修改推送状态
                            if (!db('agent_remind')
                                ->where('id', $v['id'])
                                ->update(['is_push' => 1])) {
                                throw new Exception('操作失败', RespCode::PARAMS_ERR);
                            }
                            if ($house['livingroom'] > 0 && $house['bedroom'] > 0) {
                                $name = $house['vname'] . '【' . $house['bedroom'] . '室' . $house['livingroom'] . '厅' . '】';
                            } else {
                                $name = $house['vname'];
                            }
                            $msg = $name . '于明天' . date('H:i', $house['start_time']) . '即将拍卖';
                            $cids = Aurora2::getCid($v['agent_id']);
                            if ($registration_id) {
                                if (!Aurora2::push($cids[0], $registration_id, $msg, Aurora2::TYPE3))
                                    throw new Exception('推送失败:' . $registration_id, RespCode::PARAMS_ERR);
                            } else {
                                throw new Exception('经纪人id:' . $v['agent_id'] . '没有设备号');
                            }
                        }
                        break;

                    case 2://关注的小区
                        $count = db('house')
                            ->alias('h')
                            ->leftJoin('house_auction ha', 'ha.id =h.auction_id')
                            ->leftJoin('house_village hv', 'hv.id =h.village_id')
                            ->where('h.is_shelf', 1)
                            ->where('h.village_id', $v['remind_id'])
                            ->where('h.deleted', 0)
                            ->where('ha.auction_status', 1)
                            ->whereRaw('(ha.start_time - UNIX_TIMESTAMP(now()))/60/60/24 < 1')
                            ->count();
                        $name = db('house_village')
                            ->where('id', $v['remind_id'])
                            ->value('name');
                        //有即将开拍房源
                        if ($count) {
                            //修改推送状态
                            if (!db('agent_remind')
                                ->where('id', $v['id'])
                                ->update(['is_push' => 1])) {
                                throw new Exception('操作失败', RespCode::PARAMS_ERR);
                            }
                            $msg = $name . '，明天有' . $count . '套房源即将拍卖';
                            $cids = Aurora2::getCid($v['agent_id']);
                            if ($registration_id) {
                                if (!Aurora2::push($cids[0], $registration_id, $msg, Aurora2::TYPE3))
                                    throw new Exception('推送失败:' . $registration_id, RespCode::PARAMS_ERR);
                            } else {
                                throw new Exception('经纪人id:' . $v['agent_id'] . '没有设备号', RespCode::PARAMS_ERR);
                            }
                        }
                }
            } catch (Exception $e) {
                Db::close();
                $err = "异常信息:{$e->getMessage()} | 异常所在的代码行:{$e->getFile()} | 异常文件{$e->getFile()} | php 版本 " . PHP_VERSION . "(" . PHP_OS . ")";
                Log::write($err, "PushRemindHosue_err.log");
            }
        }
        echo "end\n";
    }

}